
var key = getCookie('key');
if(key==null){
    location.href = 'login.html';
}
var loadUrl = '';
var page = 1;
$(window).scroll(function () {
    var windowheight = $(window).height();//窗口高度
    var scrolltop = $('body').scrollTop();//滚动条高度
    var eh = $(document).height();//文本高度
    if (parseInt(windowheight + scrolltop)+2 >= eh) {
        page++;
        $.ajax({
            type:"GET",
            url:loadUrl+"&page="+page,
            data:{key:key},
            dataType:"json",
            success:function(data){
                if (data){
                    var tempHtml = "";
                    for (var i=0;i<data.length;i++){
                        tempHtml += '<li><img class="img1"src='+data[i]['imageUrl']+' alt=""/><div class="center"><p>'+data[i]['goods_name']+'</p><p></p><p>价格:<span class="num">￥'+data[i]['goods_price']+'</span></p></div><img class="img2"src="../../images/yuanquan.png"alt="" onclick="addGoodsTostore('+data[i]['goods_id']+')"/></li>';
                    }
                    $('.list').html($('.list').html()+tempHtml);
                }
            }
        })
        //alert('123');
    }
})
var LoadGoodClassUrl = "/circle/index.php?act=theme&c_id=1&op=getClass&gc_parentid=0";
$.get(LoadGoodClassUrl, function (data) {
    toFillHtml(data,0)
},'json')

/**
 * 根据ID获取类别
 * @param id  父类id
 * @param Lev 分类级别
 */
function getClass(obj,Lev){
    id = obj.value;
    url = "/circle/index.php?act=theme&c_id=1&op=getClass&gc_parentid="+id;
    var select = document.getElementsByClassName('select_name');
    goodsurl = "/mobile/index.php?act=own_shop&op=goods_list";
    var gc_id = '';
    for(var i=0;i<select.length;i++){
        if(select[i].value) {
            gc_id = select[i].value;
        }
    }
    goodsurl+='&gc_id='+gc_id;
    loadUrl = goodsurl;
    $.ajax({
          type:"GET",
          url:goodsurl,
          data:{key:key},
          dataType:"json",
          success:function(goods){
                 if (goods){
                    FillGoodsDatas(goods);
                 }else{
                     $(".list").html("<p class='noData'>没有数据</p>");
                 }
          }
    })
    $.ajax({
        type:"GET",
        url:url,
        data:{key:key},
        dataType:"json",
        success:function(data){
            console.log(data);
            console.log(Lev);
            if(data){
                toFillHtml(data,Lev);
            }
        }
    })
}

function is_empty(obj){
    for (var a in obj){
        return 0
    }
    return 1
}

/**
 *
 * @param data
 * @param id
 */
function toFillHtml(data,lev){
    if (is_empty(data)){
        return false;
    }
    var box = document.getElementsByClassName('select_name')[lev];

    if(box==undefined){
        var select_name = document.createElement('select');
        select_name.className = "select_name";
        document.getElementById('select-box').appendChild(select_name);
        var box = document.getElementsByClassName('select_name')[lev];
        lev++;
        select_name.setAttribute('onchange','getClass(this,'+lev+')');
    }else{
        var box = document.getElementsByClassName('select_name')[lev];
        lev++;
        box.setAttribute('onchange','getClass(this,'+lev+')');
    }
    var html = '';
        for (var i = 0; i < data.length; i++) {
            html += "<option onclick='getClass(this,"+lev+")' value="+ data[i]['gc_id'] +">" + data[i]['gc_name'] + "</option>";
        }
        box.innerHTML = html;
}

loadGoods();

/**
 * 加载所有商品
 */
function loadGoods(){
   url = "/mobile/index.php?act=own_shop&op=goods_list";
    loadUrl =  url;
    $.ajax({
        type:"GET",
        url:url,
        data:{key:key},
        dataType:"json",
        success:function(data){
            if (data){
                var tempHtml = "";
                for (var i=0;i<data.length;i++){
                    tempHtml += '<li><img class="img1"src='+data[i]['imageUrl']+' alt=""/><div class="center"><p>'+data[i]['goods_name']+'</p><p></p><p>价格:<span class="num">￥'+data[i]['goods_price']+'</span></p></div><img class="img2"src="../../images/yuanquan.png"alt="" onclick="addGoodsTostore('+data[i]['goods_id']+')"/></li>';
                }
                $('.list').html(tempHtml);
            }
        }
    })
}

/**
 * 添加到店铺
 */
function addGoodsTostore(id){
    var addurl = "/mobile/index.php?act=own_shop&op=addGoodstoStore"
    $.ajax({
         type:"post",
         url:addurl,
         data:{key:key,id:id},
         dataType:'json',
         success:function(data){
             if(data['code']==200){
                   creatLayout("添加成功");
             }
             if(data['code']==401){
                  creatLayout('已经添加了');
             }
             if(data['code']==400){
                 creatLayout('添加失败');
             }
         }
    })
}

/**
 * 填充商品数据
 * @param data 数据
 * @constructor
 */
function FillGoodsDatas(data){
     var tempHtml = "";
     for (var i=0;i<data.length;i++){
            tempHtml += '<li><img class="img1"src='+data[i]['imageUrl']+' alt=""/><div class="center"><p>'+data[i]['goods_name']+'</p><p></p><p>价格:<span class="num">￥'+data[i]['goods_price']+'</span></p></div><img class="img2"src="../../images/yuanquan.png"alt="" onclick="addGoodsTostore('+data[i]['goods_id']+')"/></li>';
     }
    console.log(tempHtml);
     $('.list').html(tempHtml);
}


